package com.hand.exam.aspect;

import com.hand.exam.domain.MethodParam;
import com.hand.exam.service.IMethodParamService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.aspectj.lang.reflect.MethodSignature;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * @author 马旭阳
 * @date2019/7/18
 */
@Component
@Aspect
public class ServiceAspect {
    @Autowired
    private IMethodParamService methodParamService;


    @Pointcut(value = "@annotation(com.hand.exam.annoction.TestAnnoction)")
    public void aspect() {}

    @Around("aspect()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            String methodName = proceedingJoinPoint.getSignature().getName();
            String typeName = proceedingJoinPoint.getClass().getName();
            Date d = new Date();
            MethodSignature methodSignature= (MethodSignature)proceedingJoinPoint.getSignature();
            String[] strArr = methodSignature.getParameterNames();
            String s1="";
            for (String str:strArr) {
                s1 = str.concat(" ");
            }
            MethodParam methodParam = new MethodParam();
            methodParam.setDate(d);
            methodParam.setMethodName(methodName);
            methodParam.setParamName(s1);
            methodParam.setTypeName(typeName);
           methodParamService.insertMethodParam(methodParam);
            Object proceed = proceedingJoinPoint.proceed();

            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}
